<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>

<script>
	var emails = [<c:forEach items="${persons}" var="prs" varStatus="i"> 
		   		"${prs.email}"
	   			<c:if test="${!i.last}">    
             		,    
           		</c:if>
			</c:forEach>];
	function check() {
		var b = true;
		var q = document.getElementById("email");
		x = q.value;
		var cur = "${person.email}";
		if (x.toLowerCase() != cur.toLowerCase()) {
			for (var i = 0; i < emails.length; i++) {
				if (x != "" && emails[i].toLowerCase() == x.toLowerCase()) {
					if(document.getElementById("error") == null) {
						var p = document.createElement("P");
						p.id = "error";
						p.innerHTML = "Пользователь с таким E-mail'ом уже существует";
						document.body.appendChild(p);
					} 
					q.focus();
					return false;
				}
				else {
					var p = document.getElementById("error"); 
					if (p != null)
						p.remove();
				}
			}
		}
		else {
			var p = document.getElementById("error"); 
			if (p != null)
				p.remove();
		}
		return b;
	}
</script>
<title>Изменение информации о клиенте</title>
<a href="person?id=${person.id}">
	<div class="mybutton">
		Вернуться к клиенту
	</div>
</a>
<br><br><br>
<form method="POST" action="person_edit?id=${person.id}" onsubmit="return check()">
	<input name="first_name" type="text" placeholder="Имя" value="${person.first_name}"
		data-validation="required, length, custom" data-validation-length="max20"
		data-validation-regexp="^[A-Za-zА-Яа-яЁё]+$"/>
	<input name="last_name" type="text" placeholder="Фамилия" value="${person.last_name}"
		data-validation="required, length, custom" data-validation-length="max20"
		data-validation-regexp="^[A-Za-zА-Яа-яЁё]+$"/>
	<input name="middle_name" type="text" placeholder="Отчество" value="${person.middle_name}"
		data-validation="length, custom" data-validation-length="max15"
		data-validation-optional="true" data-validation-regexp="^[A-Za-zА-Яа-яЁё]+$">
	<input name="phone_number" type="text" placeholder="телефон" value="${person.phone_number}"
		data-validation="length, custom" data-validation-optional="true" 
		data-validation-length="max15" data-validation-regexp="^[0-9]+$"/>
	<input id="email" name="email" type="email" placeholder="email" value="${person.email}"
		data-validation="email, length" data-validation-length="max40"
		data-validation-optional="true"/>
	<input name="birth" type="date" placeholder="Дата рождения" value="${birth}"
		data-validation="date, required"/>
	<br>
	<input class="button" type="submit" value="Сохранить"/>
</form>
<%@ include file="/WEB-INF/jsp/footer.jsp" %>